//
//  PhoneBindVC.swift
//  LeGou
//
//  Created by lxm on 2022/7/27.
//

import UIKit

class PhoneBindVC: ZMViewController {

    //MARK:--------------------属性--------------------
    var openId : String = ""
    
    var nickname = ""
    //MARK:--------------------UI控件--------------------
    @IBOutlet weak var phone: UITextField!
    
    @IBOutlet weak var loginBtn: UIButton!
    
    //MARK:--------------------生命周期--------------------
    override func viewDidLoad() {
        super.viewDidLoad()
        setUI()
        addConstraints()
        bindAction()
    }
    //MARK:--------------------UI--------------------
    func setUI() {
        title = ""
    }
    func addConstraints() {
        
    }
    
    //MARK:--------------------methods--------------------
    func bindAction() {
        phone.rx.text.orEmpty.map({$0.zm.isPhoneNum()}).subscribe(onNext:{ [unowned self] resp in
            self.loginBtn.isEnabled = resp
            self.loginBtn.backgroundColor = resp ? Style.mainColor : Style.disableColor
        }).disposed(by: disposeBag)
    }
    
    @IBAction func onNext(_ sender: Any) {
        LoginService.shared.showVerifycation(vc: self) { success in
            if success {
                LoginAPI.sendCode(["phone" : self.phone.text!]).rxRequestNullAble(type: Any.self).subscribe(onNext:{ [unowned self] resp in
                    if let vc = router.viewController(for: "xm://main/Login/verify_input", context: ["phone" : phone.text!, "openId" : openId, "nickname" : nickname]) {
                        self.navigationController?.pushViewController(vc, animated: true)
                    }
                    
                }).disposed(by: self.disposeBag)
            }
        }
    }
    
    // 重写返回按钮
    override func customBackButton() -> UIButton? {
        let btn = ZMUtil.button(title: "返回", image: UIImage.init(named: "back_18"), font: UIFont.systemFont(ofSize: 18), color: .black)
        btn.imageEdgeInsets = UIEdgeInsets(top: 0, left: -16, bottom: 0, right: 0)
        btn.contentEdgeInsets = UIEdgeInsets(top: 0, left: 8, bottom: 0, right: 0)
        return btn
    }
    

    //MARK:--------------------delegate--------------------

}
